/**
 * 
 */
package com.yidu.drug.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.MaterialTypeModelMapper;
import com.yidu.drug.dao.MaterialsModelMapper;
import com.yidu.drug.model.MaterialTypeModel;
import com.yidu.drug.service.MaterialTypeService;

/**
 * 材料类型的service实现类
 * @author 王志伟
 * @date 2018年8月9日 下午2:12:40
 * @version 1.0
 */
@Service
public class MaterialTypeServiceImpl implements MaterialTypeService{
	
	//材料类型的dao
	@Resource
	private MaterialTypeModelMapper materialTypeDao;
	
	//材料的dao
	@Resource
	private MaterialsModelMapper materialsDao;
	/**
	 * 根据材料类型ID删除的方法
	 */
	@Override
	public int deleteByPrimaryKey(String matTypeId) {
		// TODO Auto-generated method stub
		int count=materialsDao.selectByTypeId(matTypeId);
		int rows=0;
		System.out.println(count+"根据类型ID查询出来的行数");
		if(count>0){
			return rows;
		}else{
			rows=materialTypeDao.deleteByPrimaryKey(matTypeId);
			return rows;
		}
		
	}

	/**
	 * 增加所有的材料类型信息
	 */
	@Override
	public int insert(MaterialTypeModel record) {
		// TODO Auto-generated method stub
		return materialTypeDao.insert(record);
	}

	/**
	 * 增加部分的材料类型信息
	 */
	@Override
	public int insertSelective(MaterialTypeModel record) {
		//生成ID替换横线获取前十位
		String id=Tools.getRandomUUID().substring(0,30);
		record.setMatTypeId(id);
		record.setIsva(Tools.ISVA_YES);
		//获取当前日期时间
		Timestamp ss = new Timestamp(new Date().getTime());
		record.setOptime(ss);
		return materialTypeDao.insertSelective(record);
	}

	/**
	 * 根据材料类型ID查询所有
	 */
	@Override
	public MaterialTypeModel selectByPrimaryKey(String matTypeId) {
		// TODO Auto-generated method stub
		return materialTypeDao.selectByPrimaryKey(matTypeId);
	}
	
	/**
	 * 根据材料类型主键修改部分信息
	 * 
	 */
	@Override
	public int updateByPrimaryKeySelective(MaterialTypeModel record) {
		// TODO Auto-generated method stub
		return materialTypeDao.updateByPrimaryKeySelective(record);
	}
	
	/**
	 * 根据材料类型主键修改所有信息
	 */
	@Override
	public int updateByPrimaryKey(MaterialTypeModel record) {
		// TODO Auto-generated method stub
		//获取当前时间
		Timestamp ss = new Timestamp(new Date().getTime());
		record.setOptime(ss);
		return materialTypeDao.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询所有的材料类型信息，分页
	 */
	@Override
	public Map<String,Object> findAll(String limit,String page,String matTypeName) {
		//NEW一个材料类型的模型,加入页面信息和类型名称
		MaterialTypeModel materialTypeModel = new MaterialTypeModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		materialTypeModel.setPage(pa);
		materialTypeModel.setMatTypeName(matTypeName);
		//查询所有信息或者按条件查询
		List<MaterialTypeModel> list = materialTypeDao.findAll(materialTypeModel);
		//查询出的信息的总行数
		Long count = materialTypeDao.findRows(materialTypeModel);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		return map;
	}
	
	/**
	 * 查询总行数
	 */
	@Override
	public long findRows(MaterialTypeModel record) {
		// TODO Auto-generated method stub
		return materialTypeDao.findRows(record);
	}

	@Override
	public List<MaterialTypeModel> findAllType() {
		// TODO Auto-generated method stub
		return materialTypeDao.findAllType();
	}
	
}
